/*
 * @description:
 * @Author: lal
 * @Date: 2020-05-26 16:29:31
 * @LastEditors: lal
 * @LastEditTime: 2020-08-14 15:33:34
 */

// 这里是定义菜单的地方，详情请查看 https://electronjs.org/docs/api/menu
import { dialog, BrowserWindow } from "electron";
// import log from "electron-log";
import os from "os";
import pkg from "../../../package.json";
const version = pkg.version;
const menu = [
  {
    label: "文件",
    submenu: [
      {
        label: "打开文件",
        accelerator: "CmdOrCtrl+O",
        role: "open",
        click: function() {
          openFile();
        },
      },
    ],
  },
  {
    label: "设置",
    submenu: [
      {
        label: "快速重启",
        accelerator: "F5",
        role: "reload",
      },
      {
        label: "退出",
        accelerator: "CmdOrCtrl+F4",
        role: "close",
      },
    ],
  },
  {
    label: "帮助",
    submenu: [
      {
        label: "关于",
        click: function() {
          info();
        },
      },
    ],
  },
];

// if (process.env.WEBPACK_DEV_SERVER_URL) {
menu.push({
  label: "开发者设置",
  submenu: [
    {
      label: "切换到开发者模式",
      accelerator: "F12",
      role: "toggledevtools",
    },
  ],
});
// }
function info() {
  dialog.showMessageBox({
    title: "关于",
    type: "info",
    message: "演示demo",
    detail: `版本信息：${version}\n
    引擎版本：${process.versions.v8}\n
    node版本：${process.versions.node}\n
    Chromium版本：${process.versions.chrome}\n
    electron版本：${process.versions.electron}\n
    当前系统：${os.type()} ${os.arch()} ${os.release()}`,
    noLink: true,
    buttons: ["确定"],
  });
}

async function openFile() {
  const window = BrowserWindow.getFocusedWindow();

  const result = await dialog.showOpenDialog({
    properties: ["openFile"], // properties字符串 - 包含对话框应该使用的特征，常用的是选择文件openFile，多选multiSelections，选择目录：openDirectory
  });

  if (result.canceled === false) {
    // log.info(result);
    window.webContents.send("openFiled", result.filePaths);
  }
}

export default menu;
